<?php

namespace App\Admin\Controllers;

use App\crm_deal;
use App\crm_order;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Crm_dealController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('成交单结算');
            $content->description('列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(crm_deal::class, function (Grid $grid) {

            $grid->model()->where('deal_jiesuanstatue', 2)->orwhere("deal_jiesuanstatue",3)->orderBy('deal_jiesuanstatue', 'asc');
            $grid->id('序号')->sortable();
            $grid->column("客户")->display(function () {
                return crm_order::find($this->deal_orderid)->crm_customer->customer_name;
            });
            $grid->column("电话")->display(function () {
                return crm_order::find($this->deal_orderid)->crm_customer->customer_phone;
            });
            $grid->column("线路名称")->display(function () {
                return crm_order::find($this->deal_orderid)->order_xianlu;
            });
            $grid->deal_yuangong("员工姓名");
            $grid->deal_yshoukuan("应收金额");
            $grid->deal_sshoukuan("实收金额");
            $grid->deal_sdakuan("请款金额");
            $grid->deal_ftdate("发团日期");
            $grid->deal_dealdate("下单日期");
            $grid->deal_jiesuanstatue("结算状态")->display(function ($deal_jiesuanstatue) {
                $jiesuan=['1'=>'未核算','2'=>'申请核算','3'=>'已核算'];
                return $jiesuan[$deal_jiesuanstatue];
            });
            $grid->filter(function ($filter) {
                // 去掉默认的id过滤器
                $filter->disableIdFilter();
                // 收款状态
                $filter->equal('funds_statue','请款状态')->select(['1'=>'审批中','2'=>'通过','3'=>'驳回']);
                // 收款日期
                $filter->between('funds_date', '请款日期')->datetime();
            });
            $grid->disableCreation();//禁用创建按钮
            $grid->disableRowSelector();//禁用行选择
            $grid->actions(function ($actions) {
                $actions->disableDelete();//禁用删除按钮
                $actions->disableEdit();//禁用编辑按钮
                    $actions->append('
                    <a href="'.admin_url('crm_order').'/'.$actions->row->deal_orderid.'/edit" class="btn btn-sm btn-primary">进行核算</a>  
                    ');
            });

        });
    }

    protected function form()
    {
        return Admin::form(crm_deal::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
